package com.jixiantubu.index;

import com.example.jixiantubu.R;
import com.jixiantubu.jinnang.JinNangFragment;
import com.jixiantubu.jinnang.JinNang_YouJiActivity;
import com.jixiantubu.jinnang.TopByJinNangFragment;
import com.jixiantubu.jinnang.TuiJianFragment;
import com.jixiantubu.jinnang.TopByJinNangFragment.ICallBack1;
import com.jixiantubu.luntan.JingPinFragment;
import com.jixiantubu.luntan.QuanbuFragment;
import com.jixiantubu.luntan.TopFragment;
import com.jixiantubu.luntan.TuPianFragment;
import com.jixiantubu.luntan.TopFragment.ICallBack;
import com.jixiantubu.wode.TopByWoFragment;
import com.jixiantubu.wode.WoFragment;
import com.jixiantubu.wode.TopByWoFragment.ICallBack3;
import com.jixiantubu.wode.shezhi.ShezhiActivity;
import com.jixiantubu.yueban.TopByYueBanFragment;
import com.jixiantubu.yueban.YueBanFragment;
import com.jixiantubu.yueban.TopByYueBanFragment.ICallBack2;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener,
		ICallBack1, ICallBack, ICallBack2, ICallBack3 {
	FragmentManager manager;
	FragmentTransaction transaction;
	ImageView i1, i2, i3, i4, shezhi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 去掉标题栏
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		// 初始化数据
		initData();
		// 初始化顶部导航栏
		initTop();
		// 初始化底部菜单栏
		initbottom();
		// 初始化中间内容
		
		initMiddle();

	}

	private void initData() {
		// 初始化FragmentManager
		manager = getFragmentManager();
		// 初始化底部导航栏图片
		i1 = (ImageView) findViewById(R.id.luntan);
		i2 = (ImageView) findViewById(R.id.jinnang);
		i3 = (ImageView) findViewById(R.id.yueban);
		i4 = (ImageView) findViewById(R.id.wo);
		// 添加监听器
		i1.setOnClickListener(this);
		i2.setOnClickListener(this);
		i3.setOnClickListener(this);
		i4.setOnClickListener(this);

	}

	private void initbottom() {
		// 初始化底部导航栏,底部导航栏是静态的，不需要动态嵌入
		// 设置进入首页面时图片样式
		i1.setImageResource(R.drawable.luntan_open);
		i2.setImageResource(R.drawable.jinnang);
		i3.setImageResource(R.drawable.yueban);
		i4.setImageResource(R.drawable.wo);
	}

	private void initMiddle() {
		// 动态添加中间碎片
		transaction = manager.beginTransaction();
		QuanbuFragment fragment = new QuanbuFragment();
		transaction.replace(R.id.container, fragment);
		transaction.commit();
	}

	/*private void initMiddleYouJi() {
		// 动态添加中间碎片
		transaction = manager.beginTransaction();
		QuanbuFragment fragment = new QuanbuFragment();
		// 利用Bundle传值
		
		 * Bundle bundle = new Bundle(); bundle.putString("title", "全部");
		 * fragment.setArguments(bundle);
		 
		transaction.replace(R.id.container, fragment);
		transaction.commit();
	}*/

	private void initTop() {
		// 动态添加顶部菜单栏碎片
		transaction = manager.beginTransaction();
		TopFragment fragment = new TopFragment();
		transaction.replace(R.id.topbar, fragment);
		transaction.commit();
	}

	public void shezhi(View v) {
		// 设置初始化设置监听事件
		shezhi = (ImageView) findViewById(R.id.shezhidingbu);
		shezhi.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent1 = new Intent(MainActivity.this,
						ShezhiActivity.class);
				startActivity(intent1);
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// 用来监听所有控件单击事件,单击某一张图片，及时动态修改底部导航图标
		switch (arg0.getId()) {
		case R.id.luntan:
			// 调用initTop();方法
			initTop();
			// 再次点击全部菜单时，需要重新初始化transaction
			transaction = manager.beginTransaction();
			replaceFragment(transaction, "全部", 1);
			reset();
			i1.setImageResource(R.drawable.luntan_open);
			break;
		case R.id.jinnang:
			// 调用replaceTopByJinNang()方法
			replaceTopByJinNang();
			// 再次点击锦囊菜单时，需要重新初始化transaction
			transaction = manager.beginTransaction();
			replaceFragment(transaction, "锦囊", 2);
			reset();
			i2.setImageResource(R.drawable.jinnang_open);
			break;
		case R.id.yueban:
			// 调用replaceTopByYueBan()方法
			replaceTopByYueBan();
			// 再次点击约伴菜单时，需要重新初始化transaction
			transaction = manager.beginTransaction();
			replaceFragment(transaction, "约伴", 3);
			reset();
			i3.setImageResource(R.drawable.yueban_open);
			break;
		case R.id.wo:
			// 调用replaceTopByWo()方法
			replaceTopByWo();
			// 再次点击我菜单时，需要重新初始化transaction
			transaction = manager.beginTransaction();
			replaceFragment(transaction, "我", 4);
			reset();
			i4.setImageResource(R.drawable.wo_open);
			break;
		default:
			break;
		}
		transaction.commit();
	}

	private void replaceTopByJinNang() {
		// TODO Auto-generated method stub

		transaction = manager.beginTransaction();
		TopByJinNangFragment fragment = new TopByJinNangFragment();
		transaction.replace(R.id.topbar, fragment);
		transaction.commit();

	}

	private void replaceTopByYueBan() {
		// TODO Auto-generated method stub

		transaction = manager.beginTransaction();
		TopByYueBanFragment fragment = new TopByYueBanFragment();
		transaction.replace(R.id.topbar, fragment);
		transaction.commit();

	}

	private void replaceTopByWo() {
		// TODO Auto-generated method stub

		transaction = manager.beginTransaction();
		TopByWoFragment fragment = new TopByWoFragment();
		transaction.replace(R.id.topbar, fragment);
		transaction.commit();

	}

	// 所有图片为不打开状态
	public void reset() {
		i1.setImageResource(R.drawable.luntan);
		i2.setImageResource(R.drawable.jinnang);
		i3.setImageResource(R.drawable.yueban);
		i4.setImageResource(R.drawable.wo);

	}

	// 表示单击每一个底部导航栏的时候都要替换中间内容的方法
	public void replaceFragment(FragmentTransaction transaction, String title,
			int flag) {
		Fragment fragment = null;
		Bundle bundle;
		// flag的值分别表示单击了导航栏的各个图标
		switch (flag) {
		case 1:
			fragment = new QuanbuFragment();
			break;
		case 2:
			fragment = new JinNangFragment();
			break;
		case 3:
			fragment = new YueBanFragment();
			break;
		case 4:
			fragment = new WoFragment();
			break;
		case 5:
			fragment = new JingPinFragment();
			break;
		case 6:
			fragment = new TuPianFragment();
			break;
		case 7:
			fragment = new TuiJianFragment();
			break;
		/*
		 * case 8: fragment = new YouJiFragment(); break;
		 */
		default:
			break;
		}
		// 利用Bundle传值
		bundle = new Bundle();
		bundle.putString("title", title);
		fragment.setArguments(bundle);
		transaction.replace(R.id.container, fragment);
	}

	@Override
	public void danamicLoad(String title, int flag) {
		// 需要再初始化一下transaction
		transaction = manager.beginTransaction();
		switch (flag) {
		case 1:
			// 表示全部
			replaceFragment(transaction, title, flag);
			break;
		case 5:
			// 表示精品
			replaceFragment(transaction, title, flag);
			break;
		case 6:
			// 表示图片
			replaceFragment(transaction, title, flag);
			break;
		default:
			break;
		}
		transaction.commit();
	}

	@Override
	public void danamicLoad1(String title, int flag) {
		transaction = manager.beginTransaction();
		switch (flag) {
		case 2:
			// 表示科普
			replaceFragment(transaction, title, flag);
			break;
		case 7:
			// 表示推荐
			replaceFragment(transaction, title, flag);
			break;
		case 8:
			// 表示游记
			// replaceFragment(transaction, title, flag);
			Intent intent = new Intent(MainActivity.this,
					JinNang_YouJiActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
		transaction.commit();
	}

	@Override
	public void danamicLoad2(String title, int flag) {
		transaction = manager.beginTransaction();
		switch (flag) {
		case 3:
			// 表示约伴
			replaceFragment(transaction, title, flag);
			break;
		default:
			break;
		}
		transaction.commit();
	}

	@Override
	public void danamicLoad3(String title, int flag) {
		transaction = manager.beginTransaction();
		switch (flag) {
		case 4:
			// 表示我的
			replaceFragment(transaction, title, flag);
			break;
		default:
			break;
		}
		transaction.commit();
	}

}
